﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using Sector7.Framework.Utility;
using System.Threading;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using System.Diagnostics;
using Sector7.Framework.Windows;
using Sector7.Medical.Events;

namespace Sector7.Medical.ClinicStation.Windows
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : MainWindowBase
    {
        #region Accessor
        #endregion

        #region Public functions
        /// <summary>
        /// 加载配置信息
        /// </summary>
        public override void LoadConfiguration()
        {
            base.LoadConfiguration();
            
            navigationStrip.InitializeControl(Senvironments.NavigationConfigList);
            footerStrip.InitializeControl();

            OnLoadConfigurationCompleted(new EventArgs());
        }

        /// <summary>
        /// 获取桌面控件
        /// </summary>
        /// <returns></returns>
        public override Grid GetDesk()
        {
            return grdDesk;
        }

        /// <summary>
        /// 加载模块
        /// </summary>
        /// <param name="module">模块</param>
        public override void LoadModule(Control module)
        {            
            base.LoadModule(module);

            headerStrip.UpdateTitle(module.Tag);
        }
        #endregion

        #region Private functions
        private void ExitSystem()
        {
            MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "退出系统？");

            if (result == MessageBoxResult.Yes)
            {
                this.Close();
                Environment.Exit(0);
            }
        }

        private void LoginControl_ClickClose(object sender, EventArgs e)
        {
            ExitSystem();
        }

        private void MainWindowBase_Initialized(object sender, EventArgs e)
        {
            if (SystemParameters.FullPrimaryScreenHeight < 900 ||
                SystemParameters.FullPrimaryScreenWidth < 1440)
            {
                this.WindowState = WindowState.Maximized;
                this.MaxHeight = SystemParameters.WorkArea.Height;
            }
        }

        private void HeaderStrip_ClickClose(object sender, EventArgs e)
        {
            ExitSystem();
        }

        private void NavigationStrip_ClickNavigationItem(object sender, ClickNavigationItemEventArgs e)
        {
            var navigationConfig = e.NavigationConfig;
            var moduleConfig = Senvironments.ModuleConfigList.Find(delegate(ModuleConfiguration __moduleConfig)
            {
                return __moduleConfig.ModuleId == navigationConfig.ModuleId;
            });

            if (moduleConfig != null)
            {
                string modulePath = PathUtil.Combine(moduleConfig.Path);
                UserControl module = ReflectionUtil.CreateInstance(modulePath, navigationConfig.ClassName) as UserControl;

                if (module != null)
                {
                    headerStrip.UpdateTitle(navigationConfig.Caption);
                    LoadModule(module);
                }
                else
                {
                    Alert.ShowDialog(AlertType.Error, "加载{0}模块失败！", moduleConfig.ModuleId);
                }
            }
            else
            {
                Alert.ShowDialog(AlertType.Error, "无法获取{0}模块信息！", navigationConfig.ModuleId);
            }
        }

        private void LoginControl_LoginSuccess(object sender, EventArgs e)
        {
            accountPanel.LoadAccoutInformation();

            this.Content = grdMain;
        }
        #endregion

        #region Structure
        public MainWindow()
        {            
            InitializeComponent();

            var loginControl = ReflectionUtil.CreateInstance(
                PathUtil.Combine(@"Modules\SystemManagement.dll"),
                "Sector7.Medical.SYSM.Controls.LoginControl") as LoginControlBase;
            loginControl.LoginSuccess += new EventHandler(LoginControl_LoginSuccess);
            loginControl.ClickClose += new EventHandler(LoginControl_ClickClose);

            this.Content = loginControl;
        }
        #endregion

        #region Inner members
        #endregion
    }
}
